﻿namespace NET6.WebAPI.Helpers
{
    /// <summary>
    /// 日期和时间帮助类
    /// </summary>
    public static class DateTimeHelper
    {
        public readonly static string _date = "yyyy-MM-dd";
        
        public readonly static string _dateTime = "yyyy-MM-dd HH:mm:ss";

        public readonly static string _dateChina = "yyyy 年 MM 月 dd 日";
        
        public readonly static string _dateTimeChina = "yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒";

        public readonly static string _dateTimeOfFile = "yyyyMMddHHmmssfff";

        /// <summary>
        /// 根据指定日期单位获取开始日期
        /// </summary>
        /// <returns></returns>
        public static string GetDateBegin(DateUnit unit)
        {
            var now = DateTime.Now;

            return unit switch
            {
                DateUnit.Year => now.ToString("yyyy-01-01"),
                DateUnit.Month => now.ToString("yyyy-MM-01"),
                DateUnit.Day => now.ToString("yyyy-MM-dd")
            };
        }


        /// <summary>
        /// 获取与当前时间相比的中文时间差
        /// </summary>
        /// <param name="dateTime">与当前时间比较的时间</param>
        /// <returns></returns>
        public static string GetChineseTimeDifference(DateTime dateTime)
        {
            var now = DateTime.Now;

            if (dateTime > now)
            {
                return "传入时间应比当前时间小";
            }

            var timeSpan = now - dateTime;
            var totalDays = timeSpan.TotalDays;
            var totalHours = timeSpan.TotalHours;
            var totalMinutes = timeSpan.TotalMinutes;
            var totalSeconds = timeSpan.TotalSeconds;

            return timeSpan switch
            {
                { TotalDays: >= 365 } => Math.Floor(totalDays / 365) + "年前",
                { TotalDays: >= 30 } => Math.Floor(totalDays / 30) + "个月前",
                { TotalDays: >= 7 } => Math.Floor(totalDays / 7) + "周前",
                { TotalDays: >= 1 } => Math.Floor(totalDays) + "天前",
                { TotalHours: >= 1 } => Math.Floor(totalHours) + "小时前",
                { TotalMinutes: >= 1 } => Math.Floor(totalMinutes) + "分钟前",
                { TotalSeconds: >= 1 } => Math.Floor(totalSeconds) + "秒前",
                _ => "刚刚"
            };
        }

        /// <summary>
        /// 日期单位枚举
        /// </summary>
        public enum DateUnit
        { 
            Year = 1,
            Month = 2,
            Day = 3
        }
    }
}
